<?php
/**
 *  后台管理系统-核心功能
 */
namespace app\bews\controller;
use app\bews\controller\Base;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\bews\model\BewAdminConfig;

class Bew extends Base{
	# 导航列表
	public function menuinfo(){
		$lists = Db::table('bew_admin_sys_menu')->where('parent_id',0)->order('sort DESC,smid DESC')->select();
		View::assign([
			'lists' => $lists
		]);
		return View::fetch();
	}
	# 导航添加
	public function menuadd(){
		$req = request();
		if($req->isPost()){
			$data['label'] = trim(input('post.label'));
			if(empty($data['label'])){
				View::returnCode('90000009');
			}
			$data['icon_class'] = trim(input('post.icon_class'));
			$data['sort'] = (int)trim(input('post.sort'));
			$data['status'] = (int)trim(input('post.status'));
			$data['is_show'] = (int)trim(input('post.is_show'));
			$data['remark'] = trim(input('post.remark'));
			$data['type'] = (int)trim(input('post.type',0));
			if($data['type'] == 1){
				$data['src'] = trim(input('post.src1'));
				if(empty($data['src'])){
					$this->returnCode('90000012');
				}
			}else if($data['type'] == 2){
				$data['src'] = trim(input('post.src2'));
				if(empty($data['src'])){
					$this->returnCode('90000013');
				}
			}else{
				$data['src'] = '';
			}
			// 保存用户
			$res = Db::table('bew_admin_sys_menu')->insert($data);
			if(!$res){
				$this->returnCode('91000001');
			}
			$this->returnCode(0);
		}else{
			$iconfont = Db::table('bew_z_iconfont')->where('status',1)->select();
			View::assign([
				'iconfont' => $iconfont
			]);
			return View::fetch();
		}
	}
	# 导航修改
	public function menuedit(){
		$req = request();
		if($req->isPost()){
			$smid = (int)input('post.smid');
			$data['label'] = trim(input('post.label'));
			if(!$data['label']){
				$this->returnCode('90000009');
			}
			$data['icon_class'] = trim(input('post.icon_class'));
			$data['sort'] = (int)trim(input('post.sort'));
			$data['status'] = (int)trim(input('post.status'));
			$data['is_show'] = (int)trim(input('post.is_show'));
			$data['remark'] = trim(input('post.remark'));
			$data['type'] = (int)trim(input('post.type',0));
			if($data['type'] == 1){
				$data['src'] = trim(input('post.src1'));
				if(empty($data['src'])){
					$this->returnCode('90000012');
				}
			}else if($data['type'] == 2){
				$data['src'] = trim(input('post.src2'));
				if(empty($data['src'])){
					$this->returnCode('90000013');
				}
			}else{
				$data['src'] = '';
			}
			// 保存用户
			$res = Db::table('bew_admin_sys_menu')->where('smid',$smid)->update($data);
			if(!$res){
				$this->returnCode('91000002');
			}
			$this->returnCode(0);
		}else{
			$smid = (int)input('get.smid');
			$lists = Db::table('bew_admin_sys_menu')->where('smid',$smid)->find();
			$iconfont = Db::table('bew_z_iconfont')->where('status',1)->select();
			View::assign([
				'lists' => $lists,
				'iconfont' => $iconfont
			]);
			return View::fetch();
		}
	}
	# 导航删除
	public function menudel(){
		$smid = (int)input('post.smid');
		$count = Db::table('bew_admin_sys_menu')->where('parent_id',$smid)->count();
		if($count > 0){
			$this->returnCode('90000010');
		}
		$res = Db::table('bew_admin_sys_menu')->where('smid',$smid)->delete();
		if(empty($res)){
			$this->returnCode('91000003');
		}
		$this->returnCode(0);
	}
	# 按钮管理
	public function buttoninfo(){
		$smid = (int)input('get.smid');
		$lists = Db::table('bew_admin_sys_menu')->where('parent_id',$smid)->order('sort DESC')->select()->toArray();
		if(!empty($lists)){
			foreach($lists as &$v){
				switch ($v['type']) {
					case 0:
						$v['type_name'] = '分组';
						break;
					case 1:
						$v['type_name'] = '内部跳转';
						break;
					case 2:
						$v['type_name'] = '超链接';
						break;
					default:
						$v['type_name'] = '未规划类型';
						break;
				}
			}
		}
		View::assign([
			'lists' => $lists,
			'smid'  => $smid
		]);
		return View::fetch();
	}
	# 按钮添加
	public function buttonadd(){
		$req = request();
		if($req->isPost()){
			$smid = (int)input('post.smid');
			$data['label'] = trim(input('post.label'));
			if(!$data['label']){
				$this->returnCode('90000015');
			}
			$data['icon_class'] = trim(input('post.icon_class'));
			$data['sort'] = (int)trim(input('post.sort'));
			$data['status'] = (int)trim(input('post.status'));
			$data['is_show'] = (int)trim(input('post.is_show'));
			$data['remark'] = trim(input('post.remark'));
			$data['type'] = (int)trim(input('post.type'));
			if(empty($data['type'])){
				$this->returnCode('90000011');
			}
			if($data['type'] == 1){
				$data['src'] = trim(input('post.src1'));
				if(empty($data['src'])){
					$this->returnCode('90000012');
				}
			}else if($data['type'] == 2){
				$data['src'] = trim(input('post.src2'));
				if(empty($data['src'])){
					$this->returnCode('90000013');
				}
			}
			$data['parent_id'] = $smid;
			$res = Db::table('bew_admin_sys_menu')->insert($data);
			if(!$res){
				$this->returnCode('91000001');
			}
			$this->returnCode(0);
		}else{
			$smid = (int)input('get.smid');
			$iconfont = Db::table('bew_z_iconfont')->where('status',1)->select();
			View::assign([
				'smid'  => $smid,
				'iconfont' => $iconfont
			]);
			return View::fetch();
		}
	}
	# 按钮修改
	public function buttonedit(){
		$req = request();
		if($req->isPost()){
			$smid = (int)input('post.smid');
			$data['label'] = trim(input('post.label'));
			if(!$data['label']){
				$this->returnCode('90000015');
			}
			$data['icon_class'] = trim(input('post.icon_class'));
			$data['sort'] = (int)trim(input('post.sort'));
			$data['status'] = (int)trim(input('post.status'));
			$data['is_show'] = (int)trim(input('post.is_show'));
			$data['remark'] = trim(input('post.remark'));
			$data['type'] = (int)trim(input('post.type'));
			if(empty($data['type'])){
				$this->returnCode('90000011');
			}
			if($data['type'] == 1){
				$data['src'] = trim(input('post.src1'));
				if(empty($data['src'])){
					$this->returnCode('90000012');
				}
			}else if($data['type'] == 2){
				$data['src'] = trim(input('post.src2'));
				if(empty($data['src'])){
					$this->returnCode('90000013');
				}
			}
			$res = Db::table('bew_admin_sys_menu')->where('smid',$smid)->update($data);
			if(!$res){
				$this->returnCode('91000002');
			}
			$this->returnCode(0);
		}else{
			$smid = (int)input('get.smid');
			$lists = Db::table('bew_admin_sys_menu')->where('smid',$smid)->find();
			$iconfont = Db::table('bew_z_iconfont')->where('status',1)->select();
			View::assign([
				'lists' => $lists,
				'iconfont' => $iconfont
			]);
			return View::fetch();
		}
	}
	# 按钮删除
	public function buttondel(){
		$smid = (int)input('post.smid');
		$res = Db::table('bew_admin_sys_menu')->where('smid',$smid)->delete();
		if(empty($res)){
			$this->returnCode('91000003');
		}
		$this->returnCode(0);
	}
	# 配置列表
	public function configlist(){
		$req = request();
		if($req->isPost()){
			$page = (int)input('post.page',1);
			$limit = (int)input('post.limit',$this->config['admin_page']);
			$count = Db::table('bew_admin_config')->count();
			$lists = Db::table('bew_admin_config')->page($page,$limit)->order('config_sort DESC,config_id DESC')->select();
			$this->returnCode(0,$lists,$count);
		}else{
			return View::fetch();
		}
	}
	# 配置添加
	public function configadd(){
		$req = request();
		if($req->isPost()){
			$data['config_name'] = trim(input('post.config_name'));
			if(empty($data['config_name'])){
				$this->returnCode(1,'请输入关键词');
			}
			$data['config_info'] = trim(input('post.config_info'));
			if(empty($data['config_info'])){
				$this->returnCode(1,'请输入作用');
			}
			$data['config_type'] = trim(input('post.config_type'));
			$data['config_desc'] = trim(input('post.config_desc'));
			$data['config_status'] = trim(input('post.config_status'));
			$data['config_sort'] = trim(input('post.config_sort'));
			$res = Db::table('bew_admin_config')->insert($data);
			if(empty($res)){
				$this->returnCode('91000001');
			}
			$this->returnCode(0);
		}else{
			return View::fetch();
		}
	}
	# 配置修改
	public function configedit(){
		$req = request();
		if($req->isPost()){
			$config_id = (int)input('post.config_id');
			if(empty($config_id)){
				$this->returnCode(1,'请选择一条数据');
			}
			$data['config_name'] = trim(input('post.config_name'));
			if(empty($data['config_name'])){
				$this->returnCode(1,'请输入关键词');
			}
			$data['config_info'] = trim(input('post.config_info'));
			if(empty($data['config_info'])){
				$this->returnCode(1,'请输入作用');
			}
			$data['config_type'] = trim(input('post.config_type'));
			$data['config_desc'] = trim(input('post.config_desc'));
			$data['config_status'] = trim(input('post.config_status'));
			$data['config_sort'] = trim(input('post.config_sort'));
			$res = Db::table('bew_admin_config')->where('config_id',$config_id)->update($data);
			if(empty($res)){
				$this->returnCode('91000002');
			}
			$this->returnCode(0);
		}else{
			$config_id = (int)input('get.config_id');
			$find = Db::table('bew_admin_config')->where('config_id',$config_id)->find();
			View::assign([
				'find' => $find
			]);
			return View::fetch();
		}
	}
	# 配置删除
	public function configdel(){
		$config_id = (int)input('post.config_id');
		if(empty($config_id)){
			$this->returnCode(1,'请选择一条数据');
		}
		$res = Db::table('bew_admin_config')->where('config_id',$config_id)->delete();
		if(empty($res)){
			$this->returnCode('91000003');
		}
		$this->returnCode(0);
	}
	# 配置值
	public function configvalue(){
		$req = request();
		if($req->isPost()){
			$post = input('post.');
			if(empty($post)){
				$this->returnCode(1,'数据不能为空');
			}
			$oConfig = new BewAdminConfig();
			$updateAll = $oConfig->updateAll($post);
			if(empty($updateAll)){
				$this->returnCode('91000002');
			}
			$this->returnCode(0);
		}else{
			$lists = Db::table('bew_admin_config')->order('config_sort DESC,config_id')->select();
			View::assign([
				'lists' => $lists
			]);
			return View::fetch();
		}
	}
}